package com.android.hidephotosplus;

import group.pals.android.lib.ui.lockpattern.LockPatternActivity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.android.hidephotosplus.adapter.ViewPagerAdapter;
import com.android.hidephotosplus.db.DbHelper;
import com.android.hidephotosplus.preference.IntPref;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.PasswordManager;
import com.viewpagerindicator.PageIndicator;

public class FragmentMain extends SherlockFragment implements
		OnPageChangeListener, OnNavigationListener {

	private ViewPager mViewPager;

	private PageIndicator mPageIndicator;

	private ViewPagerAdapter mViewPagerAdapter;

	private ArrayList<Fragment> mArrFragments;

	private String[] listViewPager;

	private String listNavigation[];

	private ActionBar mActionBar;

	private HiddenGalleryFragment hiddenGalleryFragment;

	private GalleryFragment galleryFragment;

	private DbHelper mDbHelper;

	public PasswordManager mPasswordManager;

	public static boolean IS_START_PASSWORD = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActionBar = getSherlockActivity().getSupportActionBar();
		mDbHelper = DbHelper.getInstance(getSherlockActivity());
		// Listnavigation
		listNavigation = getResources().getStringArray(R.array.listNavigation);
		Context context = mActionBar.getThemedContext();
		ArrayAdapter<CharSequence> list = ArrayAdapter
				.createFromResource(context, R.array.listNavigation,
						R.layout.sherlock_spinner_item);
		
		list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		mActionBar.setListNavigationCallbacks(list, this);
		mActionBar.setSelectedNavigationItem(IntPref.getPreference(
				getActivity(), Config.KEY_POSITION_LIST_NAVIGATION, 0));
		listViewPager = getResources().getStringArray(R.array.listViewPager);
		setHasOptionsMenu(true);

		// Check password
		mPasswordManager = PasswordManager.getInstances(getSherlockActivity());
		mPasswordManager.checkSetupOrUnlockPassFragment(this);
		IS_START_PASSWORD = false;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main, container, false);
		mViewPager = (ViewPager) view.findViewById(R.id.pager);

		hiddenGalleryFragment = new HiddenGalleryFragment();
		galleryFragment = new GalleryFragment();

		mArrFragments = new ArrayList<Fragment>();
		mArrFragments.add(galleryFragment);
		mArrFragments.add(hiddenGalleryFragment);

		mViewPagerAdapter = new ViewPagerAdapter(getActivity(),
				getSherlockActivity().getSupportFragmentManager(),
				mArrFragments);
		mViewPager.setAdapter(mViewPagerAdapter);

		mPageIndicator = (PageIndicator) view.findViewById(R.id.indicator);
		mPageIndicator.setViewPager(mViewPager);
		mPageIndicator.setOnPageChangeListener(this);
		return view;
	}

	@Override
	public void onPause() {
		super.onPause();
		IS_START_PASSWORD = true;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (IS_START_PASSWORD) {
			mPasswordManager.checkSetupOrUnlockPassFragment(this);
		}
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		if (getString(R.string.gallery_stock).equals(
				listViewPager[IntPref.getPreference(getActivity(),
						Config.KEY_PREF_POSITION_VIEWPAGER, 0)]))
			inflater.inflate(R.menu.fragment_stock_gallery, menu);
		else
			inflater.inflate(R.menu.fragment_hidden_gallery, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_camera:
			hiddenGalleryFragment.onClickMenuCamera();
			break;
		case R.id.menu_record:
			hiddenGalleryFragment.onClickMenuRecord();
			break;
		case R.id.menu_new_album:
			hiddenGalleryFragment.onClickMenuAddAlbum();
			break;
		case R.id.menu_add_media:
			hiddenGalleryFragment.onClickMenuAddMedia();
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		if (galleryFragment.mActionMode != null)
			galleryFragment.mActionMode.finish();
		if (hiddenGalleryFragment.mActionMode != null)
			hiddenGalleryFragment.mActionMode.finish();
		getSherlockActivity().supportInvalidateOptionsMenu();
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	public void onPageSelected(int position) {
		IntPref.setPreference(getActivity(),
				Config.KEY_PREF_POSITION_VIEWPAGER, position);
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		if (IntPref.getPreference(getSherlockActivity(),
				Config.KEY_POSITION_LIST_NAVIGATION, 0) != itemPosition) {
			galleryFragment.setupGalleryFragment(listNavigation[itemPosition]);
			hiddenGalleryFragment
					.setupHiddenGalleryFragment(listNavigation[itemPosition]);
		}
		IntPref.setPreference(getActivity(),
				Config.KEY_POSITION_LIST_NAVIGATION, itemPosition);
		return true;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Config.REQUEST_CODE_SETUP_PATTERN_PASSWORD:
			if (resultCode == Activity.RESULT_OK) {
				char[] pattern = data
						.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
				String valuePattern = new String(pattern);
				if (valuePattern != null) {
					mDbHelper.insertValue(DbHelper.KEY_PATTERN_PASSWORD,
							valuePattern);
					IS_START_PASSWORD = false;
				}

			}
			break;
		case Config.REQUEST_CODE_START_PATTERN_PASSWORD:
			if (resultCode == Activity.RESULT_OK) {
				IS_START_PASSWORD = false;
			}
			break;
		default:
			break;
		}
	}
}
